module.exports = {
  schedule: {
    cron: '30 20 6-23 * * ?',
    type: 'worker',
    env: ['pachong'],
    // immediate: true
  },

  async task(ctx) {
    ctx.logger.info('中文播客榜开始...');
    const users_xyz = await ctx.model.User.find({'froms.name': '中文播客榜'}).sort({createdAt: -1});
    let cnt_xyz = users_xyz.length;
    for (let i = 0; i < cnt_xyz; i++) {
      const user_xyz = users_xyz[i];
      let topics_xyz = await ctx.service.commonSvc.commonPodcastXyz(user_xyz.loginname);
      let result_xyz = await ctx.service.topicSvc.importTopics(topics_xyz);
      ctx.logger.info(`${i}, ${cnt_xyz}, ${user_xyz.loginname} xyz podcast ready ${topics_xyz.length} success ${result_xyz}`);
    }
    ctx.logger.info('中文播客榜结束...');


    ctx.logger.info('喜马拉雅开始...');
    const users_ximalaya = await ctx.model.User.find({'froms.name': '喜马拉雅'}).sort({createdAt: -1});
    let cnt_ximalaya = users_ximalaya.length;
    for (let i = 0; i < cnt_ximalaya; i++) {
      const user_ximalaya = users_ximalaya[i];
      let topics_ximalaya = await ctx.service.commonSvc.commonPodcastXimalaya(user_ximalaya.loginname);
      let result_ximalaya = await ctx.service.topicSvc.importTopics(topics_ximalaya);
      ctx.logger.info(`${i}, ${cnt_ximalaya}, ${user_ximalaya.loginname} ximalaya podcast ready ${topics_ximalaya.length} success ${result_ximalaya}`);
    }
    ctx.logger.info('喜马拉雅结束...');


    ctx.logger.info('播客RSS开始...');
    const users_rss = await ctx.model.User.find({
      'froms.name': {
        $eq: '播客RSS',
        $nin: ['中文播客榜', '喜马拉雅']
      }
    }).sort({createdAt: -1});
    let cnt_rss = users_rss.length;
    for (let i = 0; i < cnt_rss; i++) {
      const user_rss = users_rss[i];
      let topics_rss = await ctx.service.commonSvc.commonPodcastRss(user_rss.loginname);
      let result_rss = await ctx.service.topicSvc.importTopics(topics_rss);
      ctx.logger.info(`${i}, ${cnt_rss}, ${user_rss.loginname} rss podcast ready ${topics_rss.length} success ${result_rss}`);
    }
    ctx.logger.info('播客RSS结束...');


    let topics = await ctx.service.crawlerSvc.zhongguogequpaihangbang();
    let result = await ctx.service.topicSvc.importTopics(topics);
  }
};
